var Post = require("../../../class/user/post.c");
var User = require("../../../class/user/user.c");
var Module = require("../../../class/user/module.c");
var _Post = new Post();
var _User = new User();
var _Module = new Module();
module.exports = {
    prefix: "/user/post/",
    post: {
        "create": {
            des: {
                name: "createPost",
                content: "创建话题",
                params: [
                    {
                        key: "title",
                        type: "string"
                    },
                    {
                        key: "detail",
                        type: "string|HtmlDocument"
                    }
                ]
            },
            method: function* () {
                var self = this;
                try {
                    var data = self.request.body;
                    data ? true : (self.body = returnErrorBody("信息填写不完整"));
                    console.log(data.toString());
                    var _post = yield new Post(data, self.session.user).save();
                    self.body = _post;
                    // 创建一个话题积分+10,话题数+1
                    self.session.user = yield _User.update(self.session.user, {
                        posttotal: parseInt(self.session.user.posttotal || 0) + 1,
                        integral: parseInt(self.session.user.integral || 0) + 10
                    });
                    yield _Module.upgradepost(_post._module, true);
                } catch (e) {
                    console.log(e);
                    self.body = returnErrorBody(e.toString);
                };
            }
        }
    },
    get: {
        "listbypage": {
            des: {
                name: "getUserPostListByPage",
                content: "获取用户的话题列表",
                params: [
                    {
                        key: "page",
                        type: "Number",
                    },
                    {
                        key: "num",
                        type: "Number"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("获取用户的话题列表");
                try {
                    var data = self.request.query;
                    printData(data);
                    if (!self.session.user) {
                        printError("用户已退出登陆");
                        self.body = returnErrorBody("用户已退出登陆");
                        return;
                    };
                    var _list = yield _Post.getListByCreater(self.session.user._id, data);
                    printSuccess("获取用户话题列表成功，user_id:", self.session.user._id, ",length=", _list.length);
                    self.body = _list;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "count": {
            des: {
                name: "getUserPostCount",
                content: "获取用户话题统计",
                params: []
            },
            method: function* () {
                var self = this;
                try {
                    if (!self.session.user) {
                        self.body = printError("用户已退出登陆");
                        return;
                    };
                    var _count = yield _Post.count({ creater_id: self.session.user._id });
                    self.body = _count;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        }
    },
    delete: {
        "delete/:_id": {
            des: {
                name: "postDelete",
                content: "删除指定板块",
                params: [
                    {
                        params: "_id",
                        type: "string"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("正在删除话题");
                try {
                    var _id = self.params._id;
                    var _post = yield _Post.delete(_id);
                    self.body = _post;
                    // 删除一个话题积分-5，话题数-1
                    var posttotal = parseInt(self.session.user.posttotal || 0),
                        integral = parseInt(self.session.user.integral || 0);
                    self.session.user = yield _User.update(self.session.user, {
                        posttotal: posttotal > 0 ? posttotal - 1 : 0,
                        integral: integral > 5 ? integral - 5 : 0
                    });
                    yield _Module.upgradepost(_post._module);
                } catch (e) {
                    self.body = printError("删除话题：",e);
                };
            }
        }
    }
}
